package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.HomePager;
import com.lsy.weizhi2.JsonObject.NoteList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.ActivityDetail;
import com.lsy.weizhi2.activity.ActivityDetail2;
import com.lsy.weizhi2.activity.ActivityList;
import com.lsy.weizhi2.activity.ClassifyActivity;
import com.lsy.weizhi2.activity.CustomPage;
import com.lsy.weizhi2.activity.NoteDetail;
import com.lsy.weizhi2.activity.ShareActicity;
import com.lsy.weizhi2.activity.WebActivity;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.CostomFlag;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.customview.AutoGridView;
import com.lsy.weizhi2.customview.AutoVerticalScrollTextView;
import com.lsy.weizhi2.customview.GlideCircleTransform;
import com.lsy.weizhi2.customview.MyBanner;
import com.lsy.weizhi2.myutils.ActivityItem;
import com.lsy.weizhi2.myutils.AutoTextViewCall;
import com.lsy.weizhi2.myutils.HomeBannerCall;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.utils.MyMethod;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 嗨哟 on 2017/3/14.
 */
public class FragmentUpAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private HomePager homePager;
    private boolean isStart;
    private static int index;
    private Animation animation,animation1;
    private View bannerview,xuanshangview,classifyview,publishview,hotactivityview;
    private MyBanner myBanner;
    private List<NoteList.ListBean>list_n;
    private List<NoteList.ListBean>list_p;
    private Runnable run,run1;
    private SpannableStringBuilder[]strings;
    private SpannableStringBuilder[]stringp;
    private Handler handler=new Handler(Looper.getMainLooper());
    public FragmentUpAdapter(Context context, HomePager homePager) {
        this.context = context;
        this.homePager = homePager;
        layoutInflater=LayoutInflater.from(context);
       // bannerview=layoutInflater.inflate(R.layout.bannerview,null,false);
       // animation=AnimationUtils.loadAnimation(context,R.anim.text_out);
       // animation1=AnimationUtils.loadAnimation(context,R.anim.text_out);
        //AutoUtils.autoSize(bannerview);
        //直接new一个实例避免嵌套过多
        myBanner=new MyBanner(context);
        //子线程内计算
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                    Log.i("FragmentAdapter"," MyHeight.BannerHeight=da="+ MyHeight.BannerHeight);
                    Log.i("FragmentAdapter"," MyHeight.BannerHeight=xiao="+ (MyHeight.BannerHeight-MyHeight.ACTIONBAR));
                    myBanner.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, MyHeight.BannerHeight));
                }else{
                    Log.i("FragmentAdapter"," MyHeight.BannerHeight=xiao="+ (MyHeight.BannerHeight-MyHeight.ACTIONBAR));
                    myBanner.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, MyHeight.BannerHeight-MyHeight.ACTIONBAR));
                }
            }
        }).start();

    }

    public void UpData(int position,List<NoteList.ListBean>list_n){
        if(position==3){
            this.list_n=list_n;
            this.notifyItemChanged(position);
        }else if(position==1){
            list_p=list_n;
            Log.i("UpAdapter","list_p======"+list_p.size());
            this.notifyItemChanged(position);
        }

    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i("HomeFragmentUp","onCreate");
        xuanshangview=layoutInflater.inflate(R.layout.xuanshangview,parent,false);
        classifyview=layoutInflater.inflate(R.layout.classifyview,parent,false);
        publishview=layoutInflater.inflate(R.layout.publishview,parent,false);
        hotactivityview=layoutInflater.inflate(R.layout.hotactivityview,parent,false);
        AutoUtils.autoSize(xuanshangview);
        AutoUtils.autoSize(classifyview);
        AutoUtils.autoSize(publishview);
        AutoUtils.autoSize(hotactivityview);
        if(viewType==0){
            return new MyBannerHolder(myBanner);
        }else if(viewType==1){
            return new XuanShangHolder(xuanshangview);
        }else if(viewType==2){
            return new ClassifyHolder(classifyview);
        }else if(viewType==3){
            return new PublishHolder(publishview);
        }else if(viewType==4){
            return new HotActivity(hotactivityview);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        Log.i("HomeFragmentUp","onBind");
        if(list_n!=null&&list_n.size()!=0) {
            strings = new SpannableStringBuilder[list_n.size()];
            for (int i = 0; i < strings.length; i++) {
                String name = list_n.get(i).getRealName(), content = list_n.get(i).getTitle();
                String str = name + "发布了" + content;
                strings[i] = new SpannableStringBuilder(str);
                strings[i].setSpan(new ForegroundColorSpan(context.getResources()
                                .getColor(R.color.namecolor)), 0, name.length() + 1,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                strings[i].setSpan(new ForegroundColorSpan(context.getResources()
                                .getColor(R.color.classify_text)), name.length(), name.length() + 4,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                strings[i].setSpan(new ForegroundColorSpan(context.getResources()
                                .getColor(R.color.clicked_ok)), name.length() + 3, str.length(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        if(list_p!=null&&list_p.size()!=0){
            stringp=new SpannableStringBuilder[list_p.size()];
            for (int i = 0; i < stringp.length; i++) {
                String name = list_p.get(i).getRealName(), content = list_p.get(i).getTitle();
                String str = name + "发布了" + content;
                stringp[i] = new SpannableStringBuilder(str);
                stringp[i].setSpan(new ForegroundColorSpan(context.getResources()
                                .getColor(R.color.namecolor)), 0, name.length() + 1,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                stringp[i].setSpan(new ForegroundColorSpan(context.getResources()
                                .getColor(R.color.classify_text)), name.length(), name.length() + 4,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                stringp[i].setSpan(new ForegroundColorSpan(context.getResources()
                                .getColor(R.color.clicked_ok)), name.length() + 3, str.length(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        if(holder instanceof MyBannerHolder){
            /**
             * 临时数据
             */
            List<Integer>list=new ArrayList<Integer>();
            list.add(R.mipmap.banner1);
            list.add(R.mipmap.banner2);
            list.add(R.mipmap.banner3);
            list.add(R.mipmap.banner4);

            ((MyBannerHolder) holder).home_banner.setList(list);
            if(isStart){
                ((MyBannerHolder) holder).home_banner.start();
            }else{
                ((MyBannerHolder) holder).home_banner.stop();
            }

        }else if(holder instanceof XuanShangHolder){
            if(stringp!=null){
                Log.i("UpAdapter","stringp======"+stringp.length);
                ((XuanShangHolder) holder).home_xs_avtv.setStrings(stringp);
                if(isStart){
                    ((XuanShangHolder) holder).home_xs_avtv.startRotate();
                }else{
                    ((XuanShangHolder) holder).home_xs_avtv.stopRotate();
                }
            }
        }else if(holder instanceof ClassifyHolder){
            HomeGridAdapter homegridAdapter=new HomeGridAdapter(context);
            ((ClassifyHolder) holder).classify_agv.setAdapter(homegridAdapter);
        }else if(holder instanceof PublishHolder){
            if(strings!=null){
                SpannableStringBuilder[]strings1=new SpannableStringBuilder[strings.length];
                for (int i = 0; i < strings.length; i++) {
                    if(i==strings.length-1){
                        strings1[i]=strings[0];
                    }else{
                        strings1[i]=strings[i+1];
                    }
                }
                ((PublishHolder) holder).auto_textview_1.setStrings(strings);
                ((PublishHolder) holder).auto_textview_2.setStrings(strings1);
                if(isStart){
                    ((PublishHolder) holder).auto_textview_1.startRotate();
                    ((PublishHolder) holder).auto_textview_2.startRotate();
                }else{
                    ((PublishHolder) holder).auto_textview_1.stopRotate();
                    ((PublishHolder) holder).auto_textview_2.stopRotate();
                }
            }

        }else if(holder instanceof HotActivity){

        }
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public void start(){
        isStart=true;
        this.notifyDataSetChanged();
    }

    public void stop(){
        isStart=false;
        this.notifyDataSetChanged();
    }

    class MyBannerHolder extends RecyclerView.ViewHolder{
        MyBanner home_banner;
        public MyBannerHolder(View itemView) {
            super(itemView);
            home_banner= (MyBanner) itemView;
            home_banner.setHomeBannerCall(new HomeBannerCall() {
                @Override
                public void onClick(View view, int position) {
                    MyMethod.showToast(context,"这是第"+position+"张图片");
                    home_banner.stop();
                }
            });
        }
    }

    class XuanShangHolder extends RecyclerView.ViewHolder{
        TextView home_addr;
        AutoVerticalScrollTextView home_xs_avtv;
        public XuanShangHolder(View itemView) {
            super(itemView);
            home_addr= (TextView) itemView.findViewById(R.id.home_addr);
            home_xs_avtv= (AutoVerticalScrollTextView) itemView.findViewById(R.id.home_xs_avtv);
            home_xs_avtv.setAutoTextViewCall(new AutoTextViewCall() {
                @Override
                public void ImageChange(View view, int position) {

                }
                @Override
                public void onClick(View view,int position){
                    Gson gson=new Gson();
                    if(list_p!=null&&list_p.size()!=0){
                        String json=gson.toJson(list_p.get(position));
                        MyMethod.toActivity(context,NoteDetail.class,new String[]{"json","tag"},new String[]{json,ManagerItem.WeiZhi+""});
                    }
                }
            });
        }
    }

    class ClassifyHolder extends RecyclerView.ViewHolder{
        AutoGridView classify_agv;
        public ClassifyHolder(View itemView) {
            super(itemView);
            classify_agv= (AutoGridView) itemView.findViewById(R.id.classify_agv);
            classify_agv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    if(i==adapterView.getCount()-1){
                        CustomPage.Tag=0;
                        MyMethod.toActivity(context, CustomPage.class);
                    }else{
                        MyMethod.toActivity(context, ClassifyActivity.class,new String[]{"position"},new int[]{i});
                    }
                }
            });
        }
    }

    class PublishHolder extends RecyclerView.ViewHolder{
        ImageView huodong_iv1,huodong_iv2,hongbao_iv;
        AutoVerticalScrollTextView auto_textview_1,auto_textview_2;
        public PublishHolder(View itemView) {
            super(itemView);
            huodong_iv1= (ImageView) itemView.findViewById(R.id.huodong_iv1);
            huodong_iv2= (ImageView) itemView.findViewById(R.id.huodong_iv2);
            hongbao_iv= (ImageView) itemView.findViewById(R.id.hongbao_iv);
            auto_textview_1= (AutoVerticalScrollTextView) itemView.findViewById(R.id.auto_textview_1);
            auto_textview_2= (AutoVerticalScrollTextView) itemView.findViewById(R.id.auto_textview_2);
            hongbao_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //MyMethod.showToast(context,"点击了红包");
                    MyMethod.toActivity(context, WebActivity.class);
                   // MyMethod.toActivity(context, ShareActicity.class);
                }
            });
            auto_textview_1.setAutoTextViewCall(new AutoTextViewCall() {
                @Override
                public void ImageChange(View view, int position) {
                    if(list_n!=null&&list_n.size()!=0){
                        list_n.get(position).getAvatar();
                        Glide.with(context).load(new GlideUrl(Constants.DOWN_USER_PICTURE+list_n.get(position).getAvatar(),new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID",MyInfo.User_Id+"").build()))
                                .transform(new GlideCircleTransform(context))
                                .error(R.mipmap.default_head)
                                .into(huodong_iv1);
                    }
                }

                @Override
                public void onClick(View view, int position) {
                    if(list_n!=null&&list_n.size()!=0){
                        Gson gson=new Gson();
                        String json=gson.toJson(list_n.get(position));
                        MyMethod.toActivity(context,NoteDetail.class,new String[]{"json","tag"},new String[]{json,ManagerItem.WeiZhi+""});
                    }
                }
            });
            auto_textview_2.setAutoTextViewCall(new AutoTextViewCall() {
                @Override
                public void ImageChange(View view, int position) {
                    if(list_n!=null&&list_n.size()!=0){
                        position++;
                        if(position==list_n.size()){
                            position=0;
                        }
                        list_n.get(position).getAvatar();
                        Glide.with(context).load(new GlideUrl(Constants.DOWN_USER_PICTURE+list_n.get(position).getAvatar(),new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID",MyInfo.User_Id+"").build()))
                                .transform(new GlideCircleTransform(context))
                                .error(R.mipmap.default_head)
                                .into(huodong_iv2);
                    }

                }

                @Override
                public void onClick(View view, int position) {
                    if(list_n!=null&&list_n.size()!=0){
                        Gson gson=new Gson();
                        position++;
                        if(position==list_n.size()){
                            position=0;
                        }
                        String json=gson.toJson(list_n.get(position));
                        MyMethod.toActivity(context,NoteDetail.class,new String[]{"json","tag"},new String[]{json, ManagerItem.WeiZhi+""});
                    }

                }
            });
        }
    }

    class HotActivity extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView all_activity;
        ImageView item_1,item_2,item_3,item_4;
        public HotActivity(View itemView) {
            super(itemView);
            all_activity= (TextView) itemView.findViewById(R.id.all_activity);
            item_1= (ImageView) itemView.findViewById(R.id.item_1);
            item_2= (ImageView) itemView.findViewById(R.id.item_2);
            item_3= (ImageView) itemView.findViewById(R.id.item_3);
            item_4= (ImageView) itemView.findViewById(R.id.item_4);
            all_activity.setOnClickListener(this);
            item_1.setOnClickListener(this);
            item_2.setOnClickListener(this);
            item_3.setOnClickListener(this);
            item_4.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.all_activity:
                    CostomFlag.ActivityList_Flag = ActivityItem.Activity_All;
                    /**
                     * 暂时屏蔽，没有数据
                     */
                    MyMethod.toActivity(context, ActivityList.class);
                    break;
                case R.id.item_1:
                    CostomFlag.Activity_Flag = ActivityItem.Activity_One;
                    MyMethod.toActivity(context, ActivityDetail.class);
                    break;
                case R.id.item_2:
                    CostomFlag.Activity_Flag = ActivityItem.Activity_Two;
                    MyMethod.toActivity(context, ActivityDetail.class);
                    break;
                case R.id.item_3:
                    CostomFlag.Activity_Flag = ActivityItem.Activity_Three;
                    MyMethod.toActivity(context, ActivityDetail.class);
                    break;
                case R.id.item_4:
                    CostomFlag.Activity_Flag = ActivityItem.Activity_Fore;
                    MyMethod.toActivity(context, ActivityDetail.class);
                    break;
            }
        }
    }
}
